home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / pas_all.zip / TI159.ASC < prev    next >
Text File  |  1992-08-12  |  6KB  |  265 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.   PRODUCT : TURBO PASCAL                               NUMBER : 159
  10.   VERSION : 2.0, 3.0
  11.        OS : PC-DOS, MS-DOS, CP/M-86
  12.      DATE : May 21, 1986                                 PAGE : 1/4
  13.     TITLE : REAL FORMAT CONVERSION
  14.  
  15.  
  16.  
  17.  
  18.   The following programs may be used to convert real number data
  19.   format from that used by Turbo Pascal with 8087 support, to the
  20.   format used by regular Turbo Pascal, and vice versa. The first
  21.   program converts from 8087 to regular, and the second prog
  22.  
  23.   Program IEEErealTOregular;
  24.   {  Converts an 8-byte,  IEEE real to Turbo  Pascal's  6-byte real format. }
  25.   type
  26.     ieee       = array[1..8] of byte;
  27.     turbo_real = array[1..6] of byte;
  28.     mant       = array[1..5] of byte;
  29.     rf         = file of ieee;
  30.  
  31.   var
  32.     f : rf;
  33.  
  34.   procedure IEEEtoTurbo(long : ieee; var r : real);
  35.        { Convert from IEEE to Turbo Pascal's 6-byte real format }
  36.   var
  37.     i : integer;
  38.     e : byte;
  39.     t : turbo_real absolute r;
  40.     sign : byte;
  41.  
  42.   begin
  43.     { initialize variables }
  44.  
  45.     r := 0.0;
  46.     for i := 2 to 5 do
  47.       t[i] := 0;
  48.  
  49.     i := (long[8] and $7f) shl 4;       { get 7 highest bits of exponent }
  50.     i := i or ((long[7] and $f0) shr 4);{ get rest of exponent }
  51.  
  52.     if (i < 985) or (i > 1061) then     { check to make sure exponent }
  53.     begin                               { is in legal range }
  54.       writeln('exponent too large');
  55.       exit;
  56.     end;
  57.  
  58.     i := i - 1023;                         { take out bias }
  59.     t[1] := i + $81;                       { put in new bias }
  60.     sign := long[8] and $80;               { get sign bit }
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.   PRODUCT : TURBO PASCAL                               NUMBER : 159
  76.   VERSION : 2.0, 3.0
  77.        OS : PC-DOS, MS-DOS, CP/M-86
  78.      DATE : May 21, 1986                                 PAGE : 2/4
  79.     TITLE : REAL FORMAT CONVERSION
  80.  
  81.  
  82.  
  83.  
  84.        { build up most significant byte }
  85.  
  86.     t[6] := sign + ((long[7] and $0f) shl 3) or ((long[6] and $e0) shr 5);
  87.  
  88.     {make rest of real number in groups of 5 and 3 }
  89.  
  90.     for i := 5 downto 2 do
  91.       t[i] := ((long[i+1] and $1f) shl 3) or ((long[i] and $e0) shr 5);
  92.  
  93.   end;                                  { IEEEtoTurbo }
  94.  
  95.   var
  96.     t : ieee;
  97.     r : real;
  98.  
  99.   begin                                 { program }
  100.     assign(f, 'real87.dat');            { open input file }
  101.     reset(f);
  102.     while not eof(f) do
  103.     begin
  104.       read(f, t);                       { read an 8-byte real }
  105.       ieeetoturbo(t,  r);               { convert to a 6-byte real }
  106.       writeln(r);                       { display results }
  107.     end;
  108.   end.                                  { program }
  109.  
  110.  
  111.   program RegularTO87real;
  112.        { Converts a 6-byte real to an 8-byte, IEEE real.  }
  113.   type
  114.     ieee       = array[1..8] of byte;
  115.     turbo_real = array[1..6] of byte;
  116.     rf         = file of ieee;
  117.  
  118.   var
  119.     f : rf;
  120.     i : file of real;
  121.  
  122.   procedure TurboToIEEE(r : real; var long : ieee);
  123.        {  Converts from a 6-byte Turbo Pascal real number to an  8-
  124.           byte, IEEE format. }
  125.  
  126.   var
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.   PRODUCT : TURBO PASCAL                               NUMBER : 159
  142.   VERSION : 2.0, 3.0
  143.        OS : PC-DOS, MS-DOS, CP/M-86
  144.      DATE : May 21, 1986                                 PAGE : 3/4
  145.     TITLE : REAL FORMAT CONVERSION
  146.  
  147.  
  148.  
  149.  
  150.     realthing : turbo_real absolute r;
  151.     exp : integer;
  152.  
  153.     sign, temp : byte;
  154.     i    : integer;
  155.  
  156.   begin
  157.     fillchar(long, sizeof(long), 0);
  158.  
  159.     { build up exponent and sign of long real }
  160.  
  161.     sign := realthing[6] and $80;        { get sign bit from 6-byte }
  162.     exp := realthing[1] - $81;           { get exponent & take out bias}
  163.     exp := (exp + 1023) shl 4;           { re-bias && left justify }
  164.     exp := exp or (sign shl 8);          { put in sign }
  165.     long[8] := hi(exp);                  { insert into long real }
  166.     long[7] := lo(exp);
  167.  
  168.     { make mantissa }
  169.  
  170.     for i := 6 downto 3 do
  171.     begin
  172.  
  173.       { build up a byte }
  174.       temp := (realthing[i] and $7f shl 1) or
  175.               (realthing[i - 1] and $80 shr 7);
  176.  
  177.       { split byte and insert into long real }
  178.       long[i+1] := long[i+1] + temp and $f0 shr 4;
  179.       long[i] := long[i] + temp and $f shl 4;
  180.  
  181.     end;
  182.  
  183.     { take care of last (incomplete) byte }
  184.     temp := realthing[2] and $7f shl 1;
  185.     long[3] := long[3] + temp and $f0 shr 4;
  186.     long[2] := long[2] + temp and $f shl 4;
  187.  
  188.   end; { TurboToIEEE}
  189.   var
  190.     t : ieee;
  191.     r : real;
  192.     fname : string[80];
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.   PRODUCT : TURBO PASCAL                               NUMBER : 159
  208.   VERSION : 2.0, 3.0
  209.        OS : PC-DOS, MS-DOS, CP/M-86
  210.      DATE : May 21, 1986                                 PAGE : 4/4
  211.     TITLE : REAL FORMAT CONVERSION
  212.  
  213.  
  214.  
  215.  
  216.   begin                              { program }
  217.     write('input file name? ');      { open input file }
  218.     readln(fname);
  219.     assign(i, fname);
  220.     reset(i);
  221.     fname := '';
  222.     write('output file name? ');     { open output file }
  223.     readln(fname);
  224.     assign(f, fname);
  225.     rewrite(f);
  226.     while not eof(i) do
  227.     begin
  228.       read(i, r);                    { read a 6-byte real }
  229.       TurboToIEEE(r, t);             { convert to an 8-byte real }
  230.       write(f, t);                   { output result to file }
  231.     end;
  232.     close(f);
  233.   end.       {program}               { close output file }
  234.  
  235.   DISCLAIMER: You have the right to use this technical information
  236.   subject to the terms of the No-Nonsense License Statement that
  237.   you received with the Borland product to which this information
  238.   pertains.
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.